package com.huaruan.youdi.views.dialogs;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.huaruan.youdi.R;
import com.huaruan.youdi.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SelectMapDialog extends BaseDialogFragment {
    @BindView(R.id.ll_dismiss)
    LinearLayout ll_dismiss;
    @BindView(R.id.ll_gaode)
    LinearLayout ll_gaode;
    @BindView(R.id.ll_baidu)
    LinearLayout ll_baidu;
    @BindView(R.id.ll_close)
    LinearLayout ll_close;

    String address;

    @Override
    public void initView(View contentView, Dialog dialog) {

        ButterKnife.bind(this, contentView);
        Bundle bundle = getArguments();
        address = bundle.getString("address");

        ll_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ll_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        ll_gaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isGo("com.autonavi.minimap")) {
                    AppUtils.toastShort(getActivity(), "即将用高德地图打开导航");
                    getLocation("gaode");
                } else {
                    AppUtils.toastShort(getActivity(), "请先安装高德地图");
                }
            }
        });

        ll_baidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isGo("com.baidu.BaiduMap")) {
                    AppUtils.toastShort(getActivity(), "即将用百度地图打开导航");
                    getLocation("baidu");
                } else {
                    AppUtils.toastShort(getActivity(), "请先安装百度地图");
                }
            }
        });

    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param packageName：应用包名
     * @return
     */
    private boolean isGo(String packageName) {
        //获取packagemanager
        final PackageManager packageManager = getActivity().getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    private void getLocation(final String type) {
        GeocodeSearch geocodeSearch = new GeocodeSearch(getActivity());
        geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
                if (i == 1000) {
                    if (geocodeResult != null && geocodeResult.getGeocodeAddressList() != null &&
                            geocodeResult.getGeocodeAddressList().size() > 0) {
                        GeocodeAddress geocodeAddress = geocodeResult.getGeocodeAddressList().get(0);
                        double lat = geocodeAddress.getLatLonPoint().getLatitude();//纬度
                        double lon = geocodeAddress.getLatLonPoint().getLongitude();//经度
                        if ("baidu".equals(type)) {
                            Uri uri = Uri.parse("baidumap://map/direction?destination=latlng:" + lat + "," + lon + "|name:" + address + "&mode=driving");
                            startActivity(new Intent(Intent.ACTION_VIEW, uri));
                        } else {
                            Uri uri = Uri.parse("amapuri://route/plan/?dlat=" + lat + "&dlon=" + lon + "&dname=" + address + "&dev=0&t=0");
                            startActivity(new Intent(Intent.ACTION_VIEW, uri));
                        }
                        dismiss();
                        Log.e("高德地图lat", lat + "");
                        Log.e("高德地图lon", lon + "");
                    } else {
                        AppUtils.toastShort(getActivity(), "地名出错");
                    }

                }

            }

        });
        GeocodeQuery geocodeQuery = new GeocodeQuery(address.trim(), "29");

        geocodeSearch.getFromLocationNameAsyn(geocodeQuery);

    }

    @Override
    public int contentViewResId() {
        return R.layout.dialog_map;
    }

    @Override
    public Boolean isFullScreen() {
        return true;
    }


}
